<template>
	<div class="userBg">
		<div class="posterBox">
			<image :src="poster" mode="widthFix"></image>
			
			<div class="btnBox">
				<div class="btn" @click="changeWebview" v-if="userphone">
					<image :src="btn" mode="widthFix"></image>
				</div>
				<div class="btn2" v-else>
					<button class="flex_btn"  open-type="getPhoneNumber" @getphonenumber="getPhoneNum">
						<image :src="btn" mode="widthFix"></image>
					</button>
				</div>
			</div>
		</div>
		
		<div class="popup" v-if="showPrivacy">
			<div class="popcontent">
				<view class="privacyTit">用户隐私保护提示</view>
				<div class="poptext">感谢您的使用，在使用本小程序前，应当阅读并同意<text
						@click="handleOpenPrivacyContract">《用户隐私保护指引》</text>。当您点击同意并开始使用程序服务时，即表示您已理解并同意该条款内容，该条款将对您产生法律约束力。如您拒绝，将无法进入小程序。</div>
				<div class="btns">
					<div class="btn"><u-button shape="circle"  @tap="showPrivacy = false">不同意并退出</u-button></div>
					<div class="btn"><u-button type="error" shape="circle"  id="agree-btn" open-type="agreePrivacyAuthorization" @click="handleAgreePrivacyAuthorization">同意并继续</u-button></div>
				</div>
			</div>
		</div>
	</div>
</template>

<script>
	import {
		mapActions
	} from 'vuex';
	const db = wx.cloud.database()
	export default {
		data() {
			return {
				poster:"https://www.baixuemianshi.com/zhuanti/tingkezheng/images/poster.jpg?" + new Date(),
				btn:"https://www.baixuemianshi.com/zhuanti/tingkezheng/images/btn.png?" + new Date(),
				bjid:"",
				link:"https://www.baixuemianshi.com/zhuanti/tingkezheng/index.html",
				showPrivacy: false,
				showLoading:false
			}
		},
		onLoad(options) {
			this.bjid = options.bjid
			
			if (wx.getPrivacySetting) {
				wx.getPrivacySetting({
					success: res => {
						if (res.needAuthorization) {
							this.showPrivacy = true
						} else {
							
						}
					},
					fail: () => {},
					complete: () => {}
				})
			}
		},
		onShow() {
			if(wx.setVisualEffectOnCapture){
			    wx.setVisualEffectOnCapture({
			        visualEffect: 'hidden',
			        success:(res) => {
			            console.log('success回调',res)
			        },
			    })
			}
		},
		onHide() {
			if(wx.setVisualEffectOnCapture){
			    wx.setVisualEffectOnCapture({
			        visualEffect: 'none',
			        success:(res) => {
			            console.log('success回调',res)
			        },
			    })
			}
		},
		computed: {
			userphone() {
				return this.$store.getters.userphone ? this.$store.getters.userphone : '';
			}
		},
		created() {
			let that = this;
			
		},
		mounted() {
			let that = this 
			
		},
		watch: {
			
		},
		methods: {
			...mapActions(['LogSetPhone','LogSetOpenid']),
			getPhoneNum(e) {
				var that = this;
				uni.showToast({
					icon:"loading",
					title:"请等待"
				})
				if (e.detail.errMsg == 'getPhoneNumber:fail user deny') {
					console.log('用户取消授权');
				} else {
					uni.login({
						provider: 'weixin',
						success: function(loginRes) {
							console.log(loginRes)
							wx.cloud.callFunction({
								name: 'getuser',
								data: {
									encryptedData: e.detail.encryptedData,
									iv: e.detail.iv,
									code: loginRes.code,
									cloudID: wx.cloud.CloudID(e.detail.cloudID)
								},
								success: res => {
									console.log(res)
									let uPhone = res.result.event.cloudID.data.phoneNumber
									let openId = res.result.openId
									that.LogSetPhone(uPhone)
									that.LogSetOpenid(openId)
									that.submitePhone(uPhone, openId)
									that.changeWebview()
								},
								fail: req => {
									console.log(req)
								}
							});
						},
						fail(err) {
							uni.showToast({
								icon: 'none',
								title: '授权失败！'
							});
						}
					});
				}
			},
			submitePhone(phone, openId) {
				let that = this
				db.collection('all_user').add({
					data: {
						phone: phone,
						openId: openId,
						user: that.name,
						nickname: "",
						avatarUrl: "",
						activity: that.navTlt,
						time: new Date()
					},
					success: function(res) {
						that.showToast('success', '登录成功')
					},
					complete: function(req) {
						
					},
				})
			
			},
			
			handleAgreePrivacyAuthorization() {
				this.showPrivacy = false
			},
			handleOpenPrivacyContract() {
				// 打开隐私协议页面
				wx.openPrivacyContract({
					success: () => {}, // 打开成功
					fail: () => {}, // 打开失败
					complete: () => {}
				})
			},
			
			changeWebview(){
				
				let that = this
				console.log(that.link + '?phone=' + that.userphone + '&bjid=' + that.bjid)
				
				uni.navigateTo({
					url: '../../webview/index?href=' + encodeURIComponent(that.link + '?phone=' + that.userphone + '&bjid=' + that.bjid)
				})
			}
		},
		components: {
			
		}
	};
</script>

<style scoped>
	@import url(./index.css);
</style>